<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;

class ArticleController extends CommonController {
    public function index(){

    
		$art = D('Article'); // 实例化User对象
    	
    	$map = array(); 
    	$parameter = array();
    	
    	$title = $_GET['title'];
    	$cateid = $_GET['cate_id'];
		if (!$title == NULL){
			$map['title'] = array('like','%'.$title.'%'); 
			$parameter['title'] = $title;
			$this->assign('title',$title);
		}
    	if (!$cateid == NULL){
			$map['cate_id'] = array('eq',$cateid); 
			$parameter['cate_id'] = $cateid;
			$this->assign('cate_id',$cateid);
		}
		
		$count      = $art-> where($map)->count();// 查询满足要求的总记录数
    	$Page       = new Page($count,9,$parameter);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $art-> relation(true)-> where($map)->limit($Page->firstRow.','.$Page->listRows)->order('created_at desc')->select();
		$this->assign('data',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		
		
		$cate = M('ArticleCate');
		$catelist=$cate->getField('id,name');
		$this->assign('catelist',$catelist);
		
		$this->display('Article/index');
    }
    
    
	public function del(){
			$m=M('Article');
			$id=$_GET['id'];
			$count=$m->delete($id);
			if($count>0){
				$this->show('del');
			}else{
				$this->show('error');
			}
		}
		
		public function lock(){
			$m=M('Article');
			$id=$_GET['id'];
			$sta = $m-> where('id='.$id)->getField('status');
			if($sta>0){
				$m ->where('id='.$id)->setField('status','0');
				$this->show('unlock');
			}else{
				$m ->where('id='.$id)->setField('status','1');
				$this->show('lock');
			}
		}
		
		
	public function modify(){
			$id=$_GET['id'];
			$m=M('Article');
			$arr=$m->find($id);
			$this->assign('data',$arr);
			
			$cate = M('ArticleCate');
			$catelist=$cate->getField('id,name');
			$this->assign('catelist',$catelist);
			
			$this->display('Article/edit');
		}
		
		public function update(){
			$m=D('Article');
			
			if(!$m->create()){
				$this->show('error');
			}
			$idNum=$m->save();
			if($idNum>0){
				$this->show('ok');
			}else{
				$this->show('error');
			}
		}
		
	public function add(){
			$cate = M('ArticleCate');
			$catelist=$cate->getField('id,name');
			$this->assign('catelist',$catelist);
			$this->display('Article/add');
		}
		public function create(){
			$m=D('Article');
			if(!$m->create()){
				$this->show('error');
			}
			$idNum=$m->add();
			if($idNum>0){
				$this->show('ok');
			}else{
				$this->show('error');
			}
		}
		
		
		public function uploadimg(){
			
			$upload = new \Think\Upload();   // 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  =      '/images/'; // 设置附件上传目录
			// 上传文件 
			$info   =   $upload->uploadOne($_FILES['upload_file']);
			if(!$info) {// 上传错误提示错误信息
			    $this->show("error");
			}else{// 上传成功 获取上传文件信息
			    $path = __ROOT__.'/Uploads'.$info['savepath'].$info['savename'];
			    $result = array();
			     $result['file_path'] = $path;
			    $this->ajaxReturn($result);
			    //$this->show($path);
			}
			
			
		}
    
}